

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Date;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

import controller.Sistema;
import view.HotelDTO;
import view.ProveedorDTO;
import view.TarifaDTO;
import view.TemporadaDTO;

public class Tarifa extends HttpServlet  {
 
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;


	
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("get a Tarifa");
		this.doPost(req, resp);
	}

	
	@SuppressWarnings("deprecation")
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		System.out.println("post a Tarifa");	 	
		String msg="";
		String msgErr="";	
		
        ArrayList<HotelDTO> hoteles = Sistema.getInstancia().getHoteles();
        req.getSession().setAttribute("hoteles", hoteles);
        
        ArrayList<ProveedorDTO> proveedores = Sistema.getInstancia().buscarProveedores();
        req.getSession().setAttribute("proveedores", proveedores);        

		if ("save".equals(req.getParameter("action")))
		{
			TarifaDTO tar = new TarifaDTO();
			if (Integer.valueOf(req.getParameter("estado"))==1)
			{
				tar.setActiva(true);
			}
			else
			{
				tar.setActiva(false);
			}
			tar.setCodigo(req.getParameter("codigo"));
			tar.setNombre(req.getParameter("nombre"));
			HotelDTO hot = new HotelDTO();
			hot.setId(Integer.valueOf(req.getParameter("hotel")));
			tar.setHotel(hot);
			ProveedorDTO pro = new ProveedorDTO();					
			pro.setId(Integer.valueOf(req.getParameter("proveedor")));
			tar.setProveedor(pro);;
			tar.setMoneda(req.getParameter("moneda"));
			tar.setPension(req.getParameter("pension"));					
						
			tar.setVigenciaDesde(new Date(req.getParameter("vigenciaDesde"))); 
			tar.setVigenciaHasta(new Date(req.getParameter("vigenciaHasta")));			
			
			String jtemps = req.getParameter("temporadas");
			//System.out.print(jtemps);
			Gson gson=  new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
			
			@SuppressWarnings("unchecked")
					
			Type type = new TypeToken<ArrayList<TemporadaDTO>>(){}.getType();
			ArrayList<TemporadaDTO> temporadas = gson.fromJson(jtemps, type);
			
			for(TemporadaDTO t : temporadas)
			{				
				tar.getTemporadas().add(t);
			}
				
			tar.setId(Integer.valueOf(req.getParameter("id")));
		
			tar = Sistema.getInstancia().guardarTarifa(tar);
			req.getSession().setAttribute("tarifa", tar);						
		}		
    	else     		
    	{
    		if (req.getParameter("id")!=null)
    		{
    			TarifaDTO tar = Sistema.getInstancia().getTarifa(Integer.valueOf(req.getParameter("id")));    			
    			if (tar!=null)
    			{	
    				req.getSession().setAttribute("tarifa", tar);
    				req.getSession().setAttribute("habitaciones", tar.getHotel().getHabitaciones());
    			}
    			else
    			{    				
    				req.getSession().setAttribute("msgErr", "No se ha encontrado la tarifa");    			
    			}
    		}    
    		else
    		{
    			req.getSession().setAttribute("tarifa", new TarifaDTO());
    		}
    	} 		
 
		RequestDispatcher dispatcher = req.getRequestDispatcher("tarifa.jsp");
		dispatcher.forward(req, resp);	 
	 }
    protected void dispatch(String page, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        if (page != null)
        {
            RequestDispatcher rd = request.getRequestDispatcher(page);
            rd.forward(request, response);
        }
    }
}
